package com.filter;
import com.entity.User;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

@WebFilter("/filter/*")
public class LoginFilter extends HttpFilter {

    //private List<String> excludes=List.of("/filter");

    public void doFilter(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) throws IOException, ServletException {
        //for (String e : excludes) {
        //    if (e.equals(req.getServletPath())) {
        //        chain.doFilter(req, resp);
        //        return;
        //    }
        //}
        User user = (User)req.getSession().getAttribute("user");
        if(user!=null) {
            // 用户已登录，继续过滤链
            chain.doFilter(req, resp);
            return;
        }

            // 用户未登录，重定向到登录页面
        resp.sendRedirect(req.getContextPath() + "/login");

    }
}